<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ArrayBuild
 *
 * @author Tomek
 */
class ArrayBuild
{

    public $links = array();
    public $employees = array();
    public $jobs = array();
    public $dates = array();
    public $locations = array();
    public $jobsOffer = array();
    public $href = array();
    public $content = array();
    private $SiteName;
    private $searchLink;
    private $DOM;
    private $linksPath;
    private $hrefPath;
    private $empPath;
    private $jobPath;
    private $datePath;
    private $locPath;

    /**
     *
     * @param string $SiteName Strona z importu
     * @param string $searchLink Strona do wyszukiwarki
     * @param string $linksPath Ścieżka XPath do linków podstron
     * @param string $hrefPath Ścieżka XPath do linkiów ogłoszeń
     * @param string $empPath Ścieżka XPath do nazw pracodawców
     * @param string $jobPath Ścieżka XPath do Pracy
     * @param string $datePath Ścieżka XPath do daty
     * @param string $locPath Ścieżka XPath do lokalizacji
     * @param string $contentPath Ścieżka XPath do contentu ogłoszenia
     */
    public function __construct($SiteName, $searchLink, $linksPath = '', $hrefPath = '', $empPath = '', $jobPath = '', $datePath = '', $locPath = '', $contentPath = '')
    {
        $this->searchLink = $searchLink;
        $this->SiteName = $SiteName;
        $this->linksPath = $linksPath;
        $this->hrefPath = $hrefPath;
        $this->empPath = $empPath;
        $this->jobPath = $jobPath;
        $this->datePath = $datePath;
        $this->locPath = $locPath;
    }

    public function getLinks()
    {
        $this->links[] = $this->searchLink;
        $html = new DOMDocument();
        $html->loadHTML(file_get_contents($this->SiteName. $this->links[0]));
        $this->DOM = new DOMXPath($html);

        $result = $this->DOM->query($this->linksPath);

        foreach ($result as $r)
        {
            $this->links[] = $r->value;
        }
    }

    public function getHref()
    {
        foreach ($this->links as $l)
        {
            $html = new DOMDocument();
            $html->loadHTML(file_get_contents($this->SiteName. $l));
            $this->DOM = new DOMXPath($html);

            $linkList = $this->DOM->query($this->hrefPath);
            foreach ($linkList as $hrefElement)
            {
                $this->href[] = $hrefElement->value;
            }
        }
    }

    public function getEmp()
    {
        foreach ($this->links as $l)
        {
            $html = new DOMDocument();
            $html->loadHTML(file_get_contents($this->SiteName. $l));
            $this->DOM = new DOMXPath($html);

            $empList = $this->DOM->query($this->empPath);
            //  print_r($empList);
            foreach ($empList as $emp)
            {

                $this->employees[] = $emp->nodeValue;
            }
        }
    }

    public function getJobName()
    {
        foreach ($this->links as $l)
        {
            $html = new DOMDocument();
            $html->loadHTML(file_get_contents($this->SiteName. $l));
            $this->DOM = new DOMXPath($html);

            $jobList = $this->DOM->query($this->jobPath);

            foreach ($jobList as $job)
            {
                $this->jobs[] = $job->nodeValue;
            }
        }
    }

    public function getDates()
    {
        foreach ($this->links as $l)
        {
            $html = new DOMDocument();
            $html->loadHTML(file_get_contents($this->SiteName. $l));
            $this->DOM = new DOMXPath($html);

            $datesList = $this->DOM->query($this->datePath);

            foreach ($datesList as $date)
            {
                $this->dates[] = $date->nodeValue;
            }
        }
    }

    public function getLoc()
    {
        foreach ($this->links as $l)
        {
            $html = new DOMDocument();
            echo '<pre>';
        echo $this->SiteName. $l;
        echo '</pre>';
            $html->loadHTML(file_get_contents($this->SiteName. $l));
            $this->DOM = new DOMXPath($html);

            $locationsList = $this->DOM->query($this->locPath);

            foreach ($locationsList as $elem)
            {

                $this->locations[] = $elem->nodeValue;
            }
        }
    }

    public function getContent()
    {
        foreach ($this->links as $l)
        {
            $html = new DOMDocument();
            $html->loadHTML(file_get_contents($this->SiteName. $l));
            $this->DOM = new DOMXPath($html);



            $output = '';
            foreach ($this->href as $h)
            {
                $html = new DOMDocument();
                $html->loadHTML(file_get_contents($h));
                $XMLPath = new DOMXPath($html);
                $res = $XMLPath->query('//*[@id="Kolumne234"]');
                foreach ($res as $r)
                {
                    echo $output .= $r->nodeValue;
                }
                $this->content[] = $output;
            }
        }

        echo '<pre>';
        echo 'zal';
        var_dump($this->content);
        echo '</pre>';
    }

    public function getElements()
    {
        $this->getLinks();
        $this->getDates();
        $this->getEmp();
        $this->getHref();
        $this->getJobName();
        $this->getLinks();
        $this->getLoc();
        $this->getContent();
    }
    
    public function generateXML()
    {
        require_once 'XMLGen.php';
        $xml = new XMLGen('nazwa.xml', $employees, $jobs, $dates, $locations, $jobsOffer, $herf);
    }

}

?>
